﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class ShopDetail
    {
        partial void Shop_Loaded(bool succeeded)
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Shop);
        }

        partial void Shop_Changed()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Shop);
        }

        partial void ShopDetail_Saved()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Shop);
        }

        partial void SalesOrdersEditSelected_CanExecute(ref bool result)
        {
            result = true;
        }

        partial void SalesOrdersEditSelected_Execute()
        {
            if (SalesOrders.SelectedItem != null) this.Application.ShowSalesOrderDetail(SalesOrders.SelectedItem.Id);
        }

        partial void ShopDetail_Activated()
        {
            this.DisplayName = this.Shop.ShopName + " (Магазин)";
            try
            {
                this.FindControl("Save").IsEnabled = (this.Application.User.HasPermission(Permissions.CanEditShop)
                    || this.Application.User.HasPermission(Permissions.CanManageMyShop)
                );
            }
            catch (Exception)
            {
            }

        }


        partial void ShowAuditHistory_Execute()
        {
            this.Application.ShowAuditLogHistory("Магазины", this.Shop.Id);
        }

        partial void ShopDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
        }


        partial void ShopAddressesAddAndEditNew_Execute()
        {
            this.Application.ShowCreateNewAddress(this.ShopId,0,0,0);
        }

        partial void ShopAddressesEditSelected_CanExecute(ref bool result)
        {
            result = (ShopAddresses.SelectedItem != null);
        }

        partial void ShopAddressesEditSelected_Execute()
        {
            if (ShopAddresses.SelectedItem != null)
                this.Application.ShowAddressDetail(ShopAddresses.SelectedItem.Id);
        }

        partial void ShopVisitsEditSelected_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditVisit)
                || this.Application.User.HasPermission(Permissions.CanManageMyVisit)
            );
        }

        partial void ShopVisitsEditSelected_Execute()
        {
            if (this.ShopVisits.SelectedItem != null)
                this.Application.ShowVisitDetail(this.ShopVisits.SelectedItem.Id);
        }

        partial void ShopVisitsAddNew_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditVisit)
                || this.Application.User.HasPermission(Permissions.CanManageMyVisit)
            );
        }

        partial void ShopVisitsAddNew_Execute()
        {
            this.Application.ShowCreateNewVisit(this.Shop.Id);
        }

        partial void ShopContactsAddNew_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditContact)
                || this.Application.User.HasPermission(Permissions.CanManageMyShop)
            );
        }

        partial void ShopContactsAddNew_Execute()
        {
            this.Application.ShowCreateNewContact(0, this.Shop.Id, 0);
        }

        partial void ShopContactsEditSelected_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditContact)
                || this.Application.User.HasPermission(Permissions.CanManageMyShop)
            );
        }

        partial void ShopContactsEditSelected_Execute()
        {
            if (this.ShopContacts.SelectedItem != null)
                this.Application.ShowContactDetail(this.ShopContacts.SelectedItem.Id);
        }

        partial void SalesOrdersAddNew_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditSalesOrder)
                || this.Application.User.HasPermission(Permissions.CanManageMySalesOrder)
            );
        }

        partial void SalesOrdersAddNew_Execute()
        {
            if (this.Shop != null)
                Application.ShowCreateNewSalesOrder(this.Shop.Id);

        }
 
    }
}